package com.lzw.controller.student;

import com.lzw.anno.Log;
import com.lzw.pojo.LeaveRecord;
import com.lzw.service.student.LRS;
import com.lzw.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/LR")
public class LRC {

    @Autowired
    private LRS lrs;

    //查看请假记录
    @GetMapping("/me")
    public Map<String, Object> getMyLeaveRecords(@RequestHeader("Authorization") String token) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 解析JWT令牌，获取学生
            Claims claims = JwtUtils.parseJWT(token.replace("Bearer ", ""));
            String student = (String) claims.get("name");

            // 查询该学生的请假记录
            System.out.println("当前登录学生：" + student);
            List<LeaveRecord> leaveRecords = lrs.getLeaveRecordsByStudent(student);

            response.put("success", true);
            response.put("leaveRecords", leaveRecords);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "无效的令牌");
        }
        return response;
    }


    //请假申请
    @Log
    @PostMapping("/add")
    public String add(@RequestBody LeaveRecord leaveRecord){
        lrs.add(leaveRecord);
        return "申请成功";
    }
}
